package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author wangqi
 * @date 2023/12/28 10:47
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @PostMapping("/dish")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("添加菜品：{}",dishDTO);
        dishService.save(dishDTO);
        return Result.success();
    }

    @GetMapping("/dish/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/dish/status/{status}")
    public Result status(@PathVariable Integer status, Long id){
        log.info("修改菜品状态:status={},id={}",status,id);
        dishService.setStatus(status,id);
        return Result.success();
    }

    @PutMapping("/dish")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}",dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    @GetMapping("/dish/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    @DeleteMapping("/dish")
    public Result delete(Long[] ids){
        log.info("删除菜品：ids={}", Arrays.toString(ids));
        dishService.delete(ids);
        return Result.success();
    }

    @GetMapping("/dish/list")
    public Result list(Long categoryId){
        log.info("根据分类id查询菜品:{}",categoryId);
        List<Dish> list = dishService.getDishByCategory(categoryId);
        return Result.success(list);
    }
}
